<?php
/**
 * User: bgl
 * Date: 2023/6/13
 * Time: 16:53
 */

namespace App\Admin\Controllers\Products;

use App\Admin\Extensions\Grid\Tool\Import;
use App\Models\Product\ProductSkuClassify;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;

class SkuClassifyController extends AdminController{

    protected $title = '基础品类';

    protected function grid(){

        $grid = new Grid(new ProductSkuClassify());
        $grid->model()->orderByDesc('id');
        $grid->column('id', 'ID');
        $grid->column('classify_code', '品类编码');
        $grid->column('classify_cn', '品类名称(中文)');
        $grid->column('classify_en', '品类名称(英文)');
        $grid->column('remark', '备注');
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
        });
        $grid->tools(function (Grid\Tools $tools) {
            //$tools->append(new ImportSkuClassify());
            $tools->append(new Import('products/sku-classify-upload'));
        });

        $grid->disableRowSelector();
        $grid->disableExport();
        $grid->filter(function($filter){
            $filter->column(1/3, function ($filter){
                $filter->where(function ($query) {
                    $query->where('classify_cn', 'like', "%{$this->input}%")
                        ->orWhere('classify_en', 'like', "%{$this->input}%")
                        ->orWhere('classify_code', 'like', "%{$this->input}%");
                }, '查询','classify8')->placeholder('模糊查询品类名称，编码');
            });

        });

        return $grid;
    }

    public function form(){

        $form = new Form(new ProductSkuClassify());

        $form->hidden('id');
        $form->text('classify_cn', '品类名称(中文)')->rules('required')
            ->help('输入品类中文，例：睡垫');
        $form->text('classify_en', '品类名称(英文)')->rules('required')
            ->help('输入品类英文，例：Sleeping Pad');
        $form->text('classify_code', '品类编码')->pattern('[A-Z]{3}')->rules('required')
            ->help('品类英文三位大写字母组合，例：睡垫(Sleeping Pad），可标识为：SLP');
        $form->textarea('remark', '备注');

        $form->tools(function (Form\Tools $tools) {
            $tools->disableView();
            $tools->disableDelete();
        });

        return $form;
    }
}
